package cn.tmpan.project.service.impl;

import cn.tmpan.project.model.Nfzh;
import cn.tmpan.project.dao.NfzhMapper;
import cn.tmpan.project.service.INfzhService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AutoGenerator
 */
@Service
public class NfzhServiceImpl extends ServiceImpl<NfzhMapper, Nfzh>implements INfzhService {

    @Autowired
    private NfzhMapper nfzhMapper;

    @Value("${img.path}")
    private String realPath;

    /**
     * 查询户主
     *
     * @param hzxm  户主姓名
     * @param sfzhm 户主身份证号码
     * @return
     */
    @Override
    public Nfzh selectNfzhForLogin(String hzxm, String sfzhm) throws Exception {
        List<Nfzh> nfzhList = this.nfzhMapper.selectNfzhForLogin(hzxm, sfzhm);
        if (null == nfzhList || nfzhList.size() < 1){
            throw new Exception("身份证或户主姓名不正确");
        }
        if (nfzhList.size() > 1){
            throw new Exception("当前数据有误，请稍后再试");
        }
        return nfzhList.get(0);
    }

    /**
     * 显示图片
     *
     * @param picName 图片名称
     * @return
     */
    @Override
    public byte[] bgImage(String picName) throws Exception {
        File file = new File(realPath + picName + ".jpg");
        FileInputStream inputStream = null;
        byte[] bytes=null;
        try {
            inputStream = new FileInputStream(file);
            bytes = new byte[inputStream.available()];
            inputStream.read(bytes, 0, inputStream.available());
        } catch (FileNotFoundException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        }
        return bytes;

    }

    /**
     * 意见反馈
     *
     * @param sfzhm   身份证号
     * @param gsxyzq  公示信息是否准确  是=1  否=0
     * @param hjxxzq  户籍信息是否准确 是=1 否=0
     * @param zdtyw   宗地图有误 是=1 否=0
     * @param hcfdtyw 户产分地图有误 是=1 否=0
     * @return
     */
    @Override
    public Boolean feedBack(String sfzhm, int gsxyzq, int hjxxzq, int zdtyw, int hcfdtyw) throws Exception {
        Nfzh nfzh = this.nfzhMapper.selectById(sfzhm);
        if (null == nfzh){
            throw new Exception("当前数据有误，请稍后再试");
        }
        nfzh.setGsxyzq(gsxyzq);
        nfzh.setHjxxzq(hjxxzq);
        nfzh.setZdtyw(zdtyw);
        nfzh.setHcfdtyw(hcfdtyw);
        nfzh.setSfyqr(1); //是否确认状态设置为已确认
        int r = this.nfzhMapper.updateById(nfzh);
        return r > 0;
    }
}
